//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Validation Application Block
//===============================================================================

using System;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using EntLibContrib.Validation.Configuration;
using EntLibContrib.Validation.Properties;

namespace EntLibContrib.Validation.Validators
{
	/// <summary>
	/// Enum Defined Validator
	/// </summary>
	[ConfigurationElementType(typeof(EnumDefinedValidatorData))]
	public class EnumDefinedValidator : ValueValidator
	{
		#region Construction
		/// <summary>
		/// <para>Initializes a new instance of the <see cref="EnumDefinedValidator"/>.</para>
		/// </summary>
		public EnumDefinedValidator()
			: this(false)
		{ }

		/// <summary>
		/// <para>Initializes a new instance of the <see cref="EnumDefinedValidator"/>.</para>
		/// </summary>
		/// <param name="negated">True if the validator must negate the result of the validation.</param>
		public EnumDefinedValidator(bool negated)
			: this(null, negated)
		{ }

		/// <summary>
		/// Initializes a new instance of the <see cref="EnumDefinedValidator"/>.
		/// </summary>
		/// <param name="messageTemplate">The message template to use when logging results.</param>
		public EnumDefinedValidator(string messageTemplate)
			: this(messageTemplate, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="EnumDefinedValidator"/>.
		/// </summary>
		/// <param name="messageTemplate">The message template to use when logging results.</param>
		/// <param name="negated">True if the validator must negate the result of the validation.</param>
		public EnumDefinedValidator(string messageTemplate, bool negated)
			: base(messageTemplate, null, negated)
		{
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Implements the validation logic for the receiver.
		/// </summary>
		/// <param name="objectToValidate">The object to validate.</param>
		/// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
		/// <param name="key">The key that identifies the source of <paramref name="objectToValidate"/>.</param>
		/// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
		protected override void DoValidate(object objectToValidate, object currentTarget, string key, ValidationResults validationResults)
		{
			bool logError = false;
			bool isObjectToValidateNull = objectToValidate == null;

			if (!isObjectToValidateNull)
			{
				logError = !Enum.IsDefined(objectToValidate.GetType(), objectToValidate);
			}

			if (isObjectToValidateNull || (logError != Negated))
			{
				this.LogValidationResult(validationResults,
						GetMessage(objectToValidate, key),
						currentTarget,
						key);
			}
		}

		/// <summary>
		/// Gets the message representing a failed validation.
		/// </summary>
		/// <param name="objectToValidate">The object for which validation was performed.</param>
		/// <param name="key">The key representing the value being validated for <paramref name="objectToValidate"/>.</param>
		/// <returns>The message representing the validation failure.</returns>
		protected override string GetMessage(object objectToValidate, string key)
		{
			return string.Format(CultureInfo.CurrentUICulture,
					this.MessageTemplate,
					objectToValidate,
					key,
					this.Tag,
					objectToValidate.GetType().Name);
		}

		/// <summary>
		/// Gets the Default Message Template when the validator is non negated.
		/// </summary>
		protected override string DefaultNonNegatedMessageTemplate
		{
			get { return Resources.EnumDefinedNonNegatedDefaultMessageTemplate; }
		}

		/// <summary>
		/// Gets the Default Message Template when the validator is negated.
		/// </summary>
		protected override string DefaultNegatedMessageTemplate
		{
			get { return Resources.EnumDefinedNegatedDefaultMessageTemplate; }
		}
		#endregion
	}
}
